package cn.yunyichina.provider.base.service.impl;

import cn.yunyichina.provider.base.dao.IntroducerSettlementSettingDao;
import cn.yunyichina.provider.base.entity.IntroducerSettlementSetting;
import cn.yunyichina.provider.base.vo.IntroducerSettlementSettingVo;
import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 15-AK on 2017/6/1.
 */
@Service("introducerSettlementsettingServiceImpl")
public class IntroducerSettlementSettingServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(IntroducerSettlementSettingServiceImpl.class);
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private IntroducerSettlementSettingDao introducerSettlementSettingDao;
    public ResponseObject getIntroducerSettlementsetting(String data){
        ResponseObject responseObject = new ResponseObject();
        JSONObject request = JSONObject.parseObject(data);
        String userId = request.getString("user_id");
        if (StringUtils.isBlank(userId)){
            responseObject.setResultCodeAndMessage("1001","user_id 不能为空");
            return responseObject;
        }

        IntroducerSettlementSetting introducerSettlementSetting = new IntroducerSettlementSetting();
        introducerSettlementSetting.setUserId(userId);
        List<IntroducerSettlementSetting> introducerSettlementSettings = introducerSettlementSettingDao.select(introducerSettlementSetting);
        if (CollectionUtils.isEmpty(introducerSettlementSettings)){
            introducerSettlementSetting.setUserId("0");
            List<IntroducerSettlementSetting> defaultIntroducerSettlementSetting = introducerSettlementSettingDao.select(introducerSettlementSetting);
            responseObject.setResultCodeAndMessageAndResult("0","成功",defaultIntroducerSettlementSetting.get(0));
        }else {
            responseObject.setResultCodeAndMessageAndResult("0","成功",introducerSettlementSettings.get(0));
        }
        return responseObject;
    }

    /**
     * 新增分成比率
     * @param data
     * @return
     */
    public ResponseObject addIntroducerSettlementsetting(String data) {
        ResponseObject responseObject = new ResponseObject();
        try {
            IntroducerSettlementSettingVo request = JsonUtils.parseObject(data,IntroducerSettlementSettingVo.class);
            IntroducerSettlementSetting introducerSettlementSetting = new IntroducerSettlementSetting();
            introducerSettlementSetting.setUserId(request.getUserId());

            List<IntroducerSettlementSetting> introducerSettlementSettings = introducerSettlementSettingDao.select(introducerSettlementSetting);
            if (CollectionUtils.isEmpty(introducerSettlementSettings)){
                introducerSettlementSetting.setId(PKGenerator.generateId());
                introducerSettlementSetting.setUserName(request.getUserName());
                introducerSettlementSetting.setFirstRate(request.getFirstRate());
                introducerSettlementSetting.setSecondRate(request.getSecondRate());
                introducerSettlementSetting.setCreatedTime(simpleDateFormat.format(new Date()));
                introducerSettlementSettingDao.insert(introducerSettlementSetting);
            }else {
                IntroducerSettlementSetting dbIntroducerSettlementSetting = introducerSettlementSettings.get(0);
                dbIntroducerSettlementSetting.setUserName(request.getUserName());
                dbIntroducerSettlementSetting.setFirstRate(request.getFirstRate());
                dbIntroducerSettlementSetting.setSecondRate(request.getSecondRate());
                introducerSettlementSettingDao.update(dbIntroducerSettlementSetting);
            }

        }catch (Exception e){
            logger.error("更新或新增分成比率异常",e);
            responseObject.setResultCodeAndMessage("-1","更新或新增分成比率异常");
            return responseObject;
        }
        responseObject.setResultCodeAndMessage("0","成功");
        return responseObject;
    }
}
